/**
 * Wire is a class that emulate the behavior of a wire.
 * It has two fields, which are an OutputPin and an InputPin.
 * 
 * @author Simone D'Avico (simone.davico@usi.ch); Giuseppe Mendola (giuseppe.mendola@usi.ch);Gregory Banfi (gregory.banfi@usi.ch);
 * @version 18.05.2011
 */

package ch.usi.inf.pf2;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.util.ArrayList;

import ch.usi.inf.pf2.pins.InputPin;
import ch.usi.inf.pf2.pins.OutputPin;


public class Wire implements Component, Serializable{

	private OutputPin outputPin;
	private InputPin inputPin;
	private Component connectedComponent;

	// Fields for the GUI
	private int x1;
	private int y1;
	private int x2;
	private int y2;

	/**
	 * Constructor of Wire.
	 */
	public Wire(OutputPin outputPin, final int x1, final int y1, final int x2,
			final int y2) {
		this.outputPin = outputPin;
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	public Component getConnectedComponent() {
		return connectedComponent;
	}

	public void setConnectedComponent(Component component) {
		this.connectedComponent = component;
	}

	public void run() {
		inputPin.setValue(outputPin.getValue());
	}

	public void setInputPin(InputPin inputPin) {
		this.inputPin = inputPin;
	}

	/**
	 * This method returns the outputPin of the Wire.
	 * 
	 * @return outputPin A outputPin of the wire.
	 */
	@Override
	public OutputPin getOutputPin() {
		return outputPin;
	}

	/**
	 * This method returns the inputPin of the Wire.
	 * 
	 * @return inputPin A inputPin of the wire.
	 */
	@Override
	public InputPin getInputPin() {
		return inputPin;
	}

	/**
	 * This method passes the value from the outputPin to the inputPin.
	 * 
	 * @param outputPin
	 *            The outputPin of the wire.
	 */
	public InputPin transmit() {
		Value value = outputPin.getValue();
		InputPin inputPin = new InputPin(value);
		this.inputPin = inputPin;
		return inputPin;
	}

	@Override
	public void add(Component component) {
		// TODO Auto-generated method stub

	}

	@Override
	public void remove(Component component) {
		// TODO Auto-generated method stub

	}

	/**
	 * Methods for the GUI
	 */
	public int getX2() {
		return x2;
	}

	public int getY2() {
		return y2;
	}

	@Override
	public int getOriginX() {
		return x1 + (x2 - x1) / 2;
	}

	@Override
	public int getOriginY() {
		return y1 + (y2 - y1) / 2;
	}

	public int getAnchorX() {
		return x1;
	}

	public int getAnchorY() {
		return y1;
	}

	@Override
	public void draw(final Graphics g, Color color) {
		g.setColor(color);
		g.drawLine(x1, y1, x2, y2);
	}

	@Override
	public boolean contains(final int x, final int y) {
		return x >= Math.min(x1, x2) && x <= Math.max(x1, x2)
				&& y >= Math.min(y1, y2) && y <= Math.max(y1, y2);
	}

	public void setStartPoint(int x, int y) {
		this.x1 = x;
		this.y1 = y;
	}

	public void setFinalPoint(int x, int y) {
		this.x2 = x;
		this.y2 = y;
	}

	@Override
	public Value getValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setValue(Value value) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setInputPins(ArrayList<InputPin> inputPins) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setOutputPin(OutputPin outputPin) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getFirstFreePin() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasFreePins() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void setOrigin(int x, int y) {
		// TODO Auto-generated method stub
	}

	@Override
	public void addInConnectedWire(Wire wire) {
		// TODO Auto-generated method stub

	}

	@Override
	public ArrayList<Wire> getInConnectedWires() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addOutConnectedWire(Wire wire) {
		// TODO Auto-generated method stub

	}

	@Override
	public ArrayList<Wire> getOutConnectedWires() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<InputPin> getInputPins() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OutputPin compute(ArrayList<InputPin> inputPins) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setOutputValue() {
		// TODO Auto-generated method stub

	}

	
	public void paintComponent(Graphics g) {

		System.out.println("fffff");
	}
}
